package com.mall.controller;

import com.alibaba.fastjson.JSON;
import com.mall.service.CartService;
import com.mall.vo.WebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

//购物车controller
@RestController
@Slf4j
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /***
     * 将当前商品加入购物车中
     * @param productSkuId 商品skuId
     * @param userId 用户id
     * @return
     */
    @PostMapping("/addProductToCart")
    public WebResult addProductToCart(String productSkuId, String userId, HttpServletRequest request, HttpServletResponse response){
        WebResult result = null;
        if(userId != null && !userId.equals("")){//已登录 从redis中取数据
            cartService.addProductToCart(productSkuId, userId);
        }else{//未登录添加到cookie中
            cartService.saveProductToCookie(request,response,productSkuId);
        }
        result = WebResult.builder().code(2101).message("成功添加商品到购物车").build();
        return result;
    }

    /***
     * 获取用户的购物车
     * @param userId
     * @return
     */
    @GetMapping("/getUserCart")
    public WebResult getUserCart(String userId,HttpServletRequest request){
        WebResult<Object> result = null;
        if(userId != null && !userId.equals("")){//用户已经登陆了,从redis中获取购物车
            //这里使用Object类型的原因是，从redis中取出的数据是字符串，要转为对象就会出现时间解析错误，为了避免这种错误，直接使用Object类型即可避免
            List<Object> cartList = new ArrayList<>();
            Map<String, String> cartFromRedis = cartService.getCartFromRedis(userId);
            Set<Map.Entry<String, String>> entries = cartFromRedis.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                cartList.add(JSON.parse(entry.getValue()));
            }
            result = WebResult.builder().code(2102).data(cartList).message("获取购物车成功").build();
        }else{//未登录从cookie中获取数据
            List<Object> cartList = cartService.getProductFromCookie(request);
            result = WebResult.builder().code(2102).data(cartList).message("获取购物车成功").build();
        }
        return result;
    }

    /***
     * 删除购物车中的产品 数量大于1更新数量，数量等于1直接删除
     * @param userId
     * @param skuId
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/deleteProduct")
    public WebResult deleteProduct(String userId,String skuId,HttpServletRequest request,HttpServletResponse response){
        WebResult<Object> result = null;
        if(userId != null && !userId.equals("")){//已登陆 从redis中直接删除
            cartService.deleteProductFromRedis(userId,skuId);
            result = WebResult.builder().code(2103).message("成功删除").build();
        }else{//未登录
            cartService.deleteProductFromCookie(skuId,request,response);
            result = WebResult.builder().code(2103).message("成功删除").build();
        }
        return result;
    }

    /***
     * 直接从购物车中删除商品，不管数量多少
     * @param userId
     * @param skuId
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/deleteThisProduct")
    public WebResult deleteThisProduct(String userId,String skuId,HttpServletRequest request,HttpServletResponse response){
        WebResult<Object> result = null;
        if(userId != null && !userId.equals("")){//已登陆 从redis中直接删除
            cartService.deleteThisProductFromRedis(userId,skuId);
            result = WebResult.builder().code(2104).message("成功删除").build();
        }else{//未登录
            cartService.deleteThisProductFromCookie(skuId,request,response);
            result = WebResult.builder().code(2104).message("成功删除").build();
        }
        return result;
    }
}
